package queues;

import debugs.DomainDebug;
import entities.Domain;
import entities.GoogleAnalytic;
import entities.IpAddress;
import forms.CaptchaForm;
import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.DirectoryModel;
import models.GoogleAdsenceModel;
import models.GoogleAnalyticModel;
import models.InLinkModel;
import models.IpAddressModel;

import services.GetInfoDomain;
import services.UpdateDomain;
import systems.CurrentProcess;
import systems.RecentlyUpdate;

import admin.models.DomainModel;

public class ProcessDomainStep1 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		response.setCharacterEncoding("UTF-8");
		TreeMap check = new TreeMap();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");

		String type = (String) request.getParameter("type");
		String update = (String) request.getParameter("u");
		
		if (name == null || name.isEmpty() || !UrlHelper.checkDomain(name)) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		DomainModel domainModel = DomainModel.getInstance();
		
		Domain domain = domainModel.get(name);
		
		if (check.size() == 0) {
			
			GetInfoDomain getInfoDomain = new GetInfoDomain(domain);
			
			if (domain == null) {
			
				domain = new Domain();
				
				domain.setDomain(name);
				
				getInfoDomain.getSiteInfo();
				
				domain = getInfoDomain.getDomain();
				check = getInfoDomain.getCheck();
			}
			System.out.println("processing data domain " + name);
			//Get more info domain
			if (domain.isExists()) {
				getInfoDomain.getStep1();
				
				domain.setProcessing(false);
				domainModel.insert(domain);
				
				//Update googleAnalytic model
				if (!domain.getGoogleAnalyticId().isEmpty()) {
					GoogleAnalyticModel googleAnalyticModel = GoogleAnalyticModel.getInstance();
					googleAnalyticModel.insert(domain.getGoogleAnalyticId(), domain.getDomain());
				}
				
				//Update googleAdsense model
				if (!domain.getGoogleAdsenseId().isEmpty()) {
					GoogleAdsenceModel googleAdsenseModel = GoogleAdsenceModel.getInstance();
					googleAdsenseModel.insert(domain.getGoogleAdsenseId(), domain.getDomain());
				}
				
				//Update IpAddress Model
				if (!domain.getIpAddress().isEmpty()) {
					IpAddressModel ipAddressModel = IpAddressModel.getInstance();
					ipAddressModel.insert(domain.getIpAddress(), domain.getDomain());
				}
				
				//Update inlink
				if (domain.getOutlink().size() > 0) {
					InLinkModel inlinkModel = InLinkModel.getInstance();
					inlinkModel.insert(domain.getDomain(), domain.getOutlink());
				}
				
				DomainDebug.debugWeb(domain, response);
				
				CurrentProcess.remove(name);
				RecentlyUpdate.insert(name);
				
				//Update directory
				DirectoryModel directoryModel = DirectoryModel.getInstance();
				directoryModel.insert(domain.getDomain());
			}
		}
	}
}
